using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DataGridViewAutoFilter;
using SpoilsDataSet;

namespace SpoilsInventory
{
    public partial class MainForm : Form
    {

        BindingSource mPlaysetSource;
        BindingSource mInventorySource;
        BindingSource mCardSource;



        public MainForm()
        {
            InitializeComponent();
        }


        private void Form1_Load(object sender, EventArgs e)
        {

            tabControl1.TabPages.Remove(CardInfoTabPage);

            Common.LocalDataSet = new LocalData();

            Common.LoadEditions();
            
            mCardSource = new BindingSource(Common.LocalDataSet.CardDisplayData, "");
            mInventorySource = new BindingSource(Common.LocalDataSet.InventoryDisplayData, "");
            mPlaysetSource = new BindingSource(Common.LocalDataSet.InventoryPlaysetData, "");

            mCardSource.CurrentChanged += new EventHandler(cardBindingSource_CurrentChanged);

            
            object[] aValue = new object[1];
            aValue[0] = true;
            StatusListView.GetType().InvokeMember("DoubleBuffered", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.SetProperty, null, StatusListView, aValue);

            string aAppPath = Application.StartupPath;

            if (!Common.DataFileExists(aAppPath))
            {
                MessageBox.Show("Card Data Missing.");
                this.Close();
                this.Dispose();
            }
            else
            {
                Common.LocalDataSet.Card.ReadXml(Common.DataFilePath(aAppPath));
                Common.RefreshCardDisplayData();
            }

            if (Common.InventoryFileExists(aAppPath))
            {
                Common.LocalDataSet.Inventory.ReadXml(Common.InventoryFilePath(aAppPath));
            }

            Common.BuildInventoryList(aAppPath);


            Common.LocalDataSet.Inventory.InventoryRowChanged += new LocalData.InventoryRowChangeEventHandler(Inventory_InventoryRowChanged);

            CardInfoGrid.AutoGenerateColumns = false;

            CardInfoGrid.Columns[CardInfoGrid.Columns.Add("Edition", "Edition")].DataPropertyName = "Edition";
            CardInfoGrid.Columns[CardInfoGrid.Columns.Add("EditionName", "Edition")].DataPropertyName = "EditionName";
            CardInfoGrid.Columns[CardInfoGrid.Columns.Add("Number", "Number")].DataPropertyName = "Number";
            CardInfoGrid.Columns[CardInfoGrid.Columns.Add("Trade", "Trade")].DataPropertyName = "Trade";
            CardInfoGrid.Columns[CardInfoGrid.Columns.Add("Name", "Name")].DataPropertyName = "Name";
            CardInfoGrid.Columns[CardInfoGrid.Columns.Add("Rarity", "Rarity")].DataPropertyName = "Rarity";

            CardInfoGrid.Columns[CardInfoGrid.Columns.Add("Cost", "Cost")].DataPropertyName = "Cost";
            CardInfoGrid.Columns[CardInfoGrid.Columns.Add("Threshold", "Threshold")].DataPropertyName = "Threshold";
            CardInfoGrid.Columns[CardInfoGrid.Columns.Add("ThresholdType", "ThresholdType")].DataPropertyName = "ThresholdType";
            CardInfoGrid.Columns[CardInfoGrid.Columns.Add("Type", "Type")].DataPropertyName = "Type";
            CardInfoGrid.Columns[CardInfoGrid.Columns.Add("Subtype", "Subtype")].DataPropertyName = "Subtype";
            CardInfoGrid.Columns[CardInfoGrid.Columns.Add("Strength", "Strength")].DataPropertyName = "Strength";
            CardInfoGrid.Columns[CardInfoGrid.Columns.Add("Life", "Life")].DataPropertyName = "Life";
            CardInfoGrid.Columns[CardInfoGrid.Columns.Add("Speed", "Speed")].DataPropertyName = "Speed";
            CardInfoGrid.Columns[CardInfoGrid.Columns.Add("CardText", "CardText")].DataPropertyName = "CardText";

            CardInfoGrid.Sort(CardInfoGrid.Columns["Edition"], ListSortDirection.Ascending);
            CardInfoGrid.Columns["Edition"].Visible = false;

            CardInfoGrid.BindingContextChanged += new EventHandler(CardInfoGrid_BindingContextChanged);
            CardInfoGrid.CellFormatting += new DataGridViewCellFormattingEventHandler(Common.FormatGridCell);


            Common.SetGridColumnOrder(Application.StartupPath,this.Name, CardInfoGrid);

            CardInfoGrid.DataSource = mCardSource;

            

            

            InventoryGrid.CellFormatting += new DataGridViewCellFormattingEventHandler(InventoryGrid_CellFormatting);
            InventoryGrid.CellFormatting += new DataGridViewCellFormattingEventHandler(Common.FormatGridCell);

            InventoryGrid.AutoGenerateColumns = false;

            
            InventoryGrid.Columns[InventoryGrid.Columns.Add("Edition", "Edition")].DataPropertyName = "Edition";
            InventoryGrid.Columns[InventoryGrid.Columns.Add("EditionName", "Edition")].DataPropertyName = "EditionName";
            InventoryGrid.Columns["EditionName"].ReadOnly = true;

            InventoryGrid.Columns[InventoryGrid.Columns.Add("CardName", "Name")].DataPropertyName = "CardName";
            InventoryGrid.Columns["CardName"].ReadOnly = true;
            InventoryGrid.Columns[InventoryGrid.Columns.Add("Number", "Number")].DataPropertyName = "Number";
            InventoryGrid.Columns[InventoryGrid.Columns.Add("NormalCount", "Normal")].DataPropertyName = "NormalCount";
            InventoryGrid.Columns[InventoryGrid.Columns.Add("FoilCount", "Foil")].DataPropertyName = "FoilCount";
            InventoryGrid.Columns[InventoryGrid.Columns.Add("Notes", "Notes")].DataPropertyName = "Notes";

            InventoryGrid.Sort(InventoryGrid.Columns["Edition"], ListSortDirection.Ascending);
            InventoryGrid.Columns["Edition"].Visible = false;

            InventoryGrid.BindingContextChanged += new EventHandler(InventoryGrid_BindingContextChanged);

            Common.SetGridColumnOrder(Application.StartupPath, this.Name, InventoryGrid);

            InventoryGrid.DataSource = mInventorySource;

          


            PlaysetGrid.CellFormatting += new DataGridViewCellFormattingEventHandler(PlaysetGrid_CellFormatting);
            PlaysetGrid.CellFormatting += new DataGridViewCellFormattingEventHandler(Common.FormatGridCell);


            PlaysetGrid.AutoGenerateColumns = false;

            PlaysetGrid.Columns[PlaysetGrid.Columns.Add("Edition", "Edition")].DataPropertyName = "Edition";
            PlaysetGrid.Columns[PlaysetGrid.Columns.Add("EditionName", "Edition")].DataPropertyName = "EditionName";
            PlaysetGrid.Columns[PlaysetGrid.Columns.Add("Number", "Number")].DataPropertyName = "Number";
            PlaysetGrid.Columns[PlaysetGrid.Columns.Add("CardName", "Name")].DataPropertyName = "CardName";
            PlaysetGrid.Columns[PlaysetGrid.Columns.Add("Rarity", "Rarity")].DataPropertyName = "Rarity";
            PlaysetGrid.Columns[PlaysetGrid.Columns.Add("NormalCount", "Normal")].DataPropertyName = "NormalCount";
            PlaysetGrid.Columns[PlaysetGrid.Columns.Add("NormalExtra", "Normal Extras")].DataPropertyName = "NormalExtra";
            PlaysetGrid.Columns[PlaysetGrid.Columns.Add("FoilCount", "Foil")].DataPropertyName = "FoilCount";
            PlaysetGrid.Columns[PlaysetGrid.Columns.Add("FoilExtra", "Foil Extras")].DataPropertyName = "FoilExtra";
            PlaysetGrid.Columns[PlaysetGrid.Columns.Add("TotalCount", "Total")].DataPropertyName = "TotalCount";
            PlaysetGrid.Columns[PlaysetGrid.Columns.Add("TotalExtra", "Total Extras")].DataPropertyName = "TotalExtra";
            PlaysetGrid.Columns[PlaysetGrid.Columns.Add("Notes", "Notes")].DataPropertyName = "Notes";

            PlaysetGrid.Sort(PlaysetGrid.Columns["Edition"], ListSortDirection.Ascending);
            PlaysetGrid.Columns["Edition"].Visible = false;

            PlaysetGrid.BindingContextChanged += new EventHandler(PlaysetGrid_BindingContextChanged);

            Common.SetGridColumnOrder(Application.StartupPath, this.Name, PlaysetGrid);

            PlaysetGrid.DataSource = mPlaysetSource;

            

         
            CardTextTextBox.DataBindings.Add("Text", mCardSource, "CardText");
            CardNameLabel.DataBindings.Add("Text", mCardSource, "Name");
            CardNumberTextBox.DataBindings.Add("Text", mCardSource, "Number");



        }

        void PlaysetGrid_BindingContextChanged(object sender, EventArgs e)
        {
            PlaysetGrid.Columns["EditionName"].HeaderCell = new DataGridViewAutoFilter.DataGridViewAutoFilterColumnHeaderCell(PlaysetGrid.Columns["EditionName"].HeaderCell);
            PlaysetGrid.Columns["Rarity"].HeaderCell = new DataGridViewAutoFilter.DataGridViewAutoFilterColumnHeaderCell(PlaysetGrid.Columns["Rarity"].HeaderCell);
        }

        void InventoryGrid_BindingContextChanged(object sender, EventArgs e)
        {
            InventoryGrid.Columns["EditionName"].HeaderCell = new DataGridViewAutoFilter.DataGridViewAutoFilterColumnHeaderCell(InventoryGrid.Columns["EditionName"].HeaderCell);
        }

        void PlaysetGrid_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {


            if (e.RowIndex == -1)
                return;


            DataGridView aGrid = (DataGridView)sender;


            FormatInventoryGrid(aGrid, e);

            

            if (((int)aGrid.Rows[e.RowIndex].Cells["NormalCount"].Value) == 4)
            {
                e.CellStyle.BackColor = Color.White;
            }

            if (((int)aGrid.Rows[e.RowIndex].Cells["NormalCount"].Value) < 4)
            {
                e.CellStyle.BackColor = Color.OrangeRed;
            }

            if (((int)aGrid.Rows[e.RowIndex].Cells["NormalExtra"].Value) > 0)
            {
                e.CellStyle.BackColor = Color.LightBlue;
            }


        }

        void InventoryGrid_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            DataGridView aGrid = (DataGridView)sender;

            FormatInventoryGrid(aGrid, e);
           
        }

        void FormatInventoryGrid(DataGridView theGrid, DataGridViewCellFormattingEventArgs e)
        {
            //if (theGrid.Columns[e.ColumnIndex].Name == "CardName")
            //{
            //    e.FormattingApplied = true;
            //    e.Value = Common.LocalDataSet.Card.FindByEditionNumber((int)theGrid.Rows[e.RowIndex].Cells["Edition"].Value, (int)theGrid.Rows[e.RowIndex].Cells["Number"].Value).Name;
            //}

            //if (theGrid.Columns[e.ColumnIndex].Name == "Rarity")
            //{
            //    e.FormattingApplied = true;
            //    e.Value = Common.LocalDataSet.Card.FindByEditionNumber((int)theGrid.Rows[e.RowIndex].Cells["Edition"].Value, (int)theGrid.Rows[e.RowIndex].Cells["Number"].Value).Rarity;
            //}

            //if (theGrid.Columns[e.ColumnIndex].Name == "EditionName")
            //{
            //    e.FormattingApplied = true;
            //    e.Value = Common.LocalDataSet.Editions.FindByNumber((int)theGrid.Rows[e.RowIndex].Cells["Edition"].Value).Name;
            //}
        }

        void InventoryGrid_CellValuePushed(object sender, DataGridViewCellValueEventArgs e)
        {
            int aColumnIndex = e.ColumnIndex;
            if (aColumnIndex > 1)
                aColumnIndex -= 1;

            LocalData.InventoryRow aInventoryRow = Common.LocalDataSet.Inventory[e.RowIndex];
            aInventoryRow[aColumnIndex] = e.Value;
        }

        void InventoryGrid_CellValueNeeded(object sender, DataGridViewCellValueEventArgs e)
        {
            int aColumnIndex = e.ColumnIndex;
            if (aColumnIndex > 1)
                aColumnIndex -= 1;
            LocalData.InventoryRow aInventoryRow = Common.LocalDataSet.Inventory[e.RowIndex];
            switch (e.ColumnIndex)
            {
                case 1:
                    e.Value = Common.LocalDataSet.Card.FindByEditionNumber(aInventoryRow.Edition, aInventoryRow.Number).Name;    
                    break;
                default :
                    e.Value = aInventoryRow[aColumnIndex];
                    break;
            }
        }

        void Inventory_InventoryRowChanged(object sender, LocalData.InventoryRowChangeEvent e)
        {
            e.Row.EndEdit();            
            Common.SaveInventory(Application.StartupPath);
        }

        void cardBindingSource_CurrentChanged(object sender, EventArgs e)
        {
            if (mCardSource.Current != null)
            {
                LocalData.CardDisplayDataRow aRow = (LocalData.CardDisplayDataRow)((DataRowView)mCardSource.Current).Row;

                string aFileName = Common.ImagePath(Application.StartupPath, aRow.Edition, aRow.Name);

                if (System.IO.File.Exists(aFileName))
                    pictureBox1.Image = Image.FromFile(aFileName);
                else
                    pictureBox1.Image = null;
            }
            else
            {
                pictureBox1.Image = null;
            }
        }











        //      void RefreshFirstEditionPartTwo()
        //{
        //    StringBuilder aCardPage = new StringBuilder();
        //    String[] aCardInfo;

        //    System.Net.HttpWebRequest aRequest = (System.Net.HttpWebRequest)System.Net.WebRequest.Create();
        //    byte[] aBuffer = new byte[8092];

        //    System.IO.Stream aResponseStream = ((System.Net.HttpWebResponse)(aRequest.GetResponse())).GetResponseStream();

        //    int aCount = 0;
        //    do
        //    {
        //        aCount = aResponseStream.Read(aBuffer, 0, aBuffer.Length);
        //        // make sure we read some data
        //        if (aCount != 0)
        //        {
        //            // translate from bytes to ASCII text
        //            aCardPage.Append(Encoding.ASCII.GetString(aBuffer, 0, aCount));
        //        }
        //    }
        //    while (aCount > 0); // any more data to read?

        //    aCardInfo = System.Text.RegularExpressions.Regex.Split(aCardPage.ToString(), "<hr>");

        //    for (int i = 1; i < aCardInfo.Length; i++)
        //    {
        //        int aFirstPos = 0;
        //        int aSecondPos = 0;

        //        aFirstPos = aCardInfo[i].IndexOf("<b>");
        //        if (aFirstPos > -1)
        //        {
        //            aSecondPos = aCardInfo[i].IndexOf("</b>", aFirstPos);
        //            String aName = aCardInfo[i].Substring(aFirstPos + 3, aSecondPos - aFirstPos - 3);

        //            aFirstPos = aCardInfo[i].IndexOf(System.Environment.NewLine, aSecondPos);
        //            aSecondPos = aCardInfo[i].IndexOf("<br>", aFirstPos);
        //            String aRarity = aCardInfo[i].Substring(aFirstPos + 2, aSecondPos - aFirstPos - 2);

        //            aFirstPos = aCardInfo[i].IndexOf(System.Environment.NewLine, aSecondPos);
        //            aSecondPos = aFirstPos + 3;
        //            int aCost = Convert.ToInt32(aCardInfo[i].Substring(aFirstPos + 2, 3).Trim());

        //            aFirstPos = aSecondPos;
        //            aSecondPos = aCardInfo[i].IndexOf(System.Environment.NewLine, aFirstPos);

        //            System.Text.RegularExpressions.MatchCollection aMatches = System.Text.RegularExpressions.Regex.Matches(aCardInfo[i].Substring(aFirstPos, aSecondPos - aFirstPos), "/*.jpg");
        //            int aThreshold = aMatches.Count;

        //            aSecondPos = aCardInfo[i].IndexOf(".jpg");
        //            aFirstPos = aCardInfo[i].Substring(0, aSecondPos).LastIndexOf("/");
        //            String aThresholdType = aCardInfo[i].Substring(aFirstPos + 1, aSecondPos - aFirstPos - 1);
        //            aThresholdType = aThresholdType[0].ToString().ToUpper() + aThresholdType.Substring(1);


        //            aFirstPos = aCardInfo[i].IndexOf(System.Environment.NewLine, aSecondPos);
        //            aSecondPos = aCardInfo[i].IndexOf("<br>", aFirstPos);
        //            String aType = aCardInfo[i].Substring(aFirstPos + 2, aSecondPos - aFirstPos - 2);


        //             int aStrength = -1;
        //            int aLife = -1;
        //            int aSpeed = -1;

        //            if(aType.StartsWith("Character",StringComparison.OrdinalIgnoreCase))
        //            {
        //                aFirstPos = aCardInfo[i].IndexOf(System.Environment.NewLine, aSecondPos);
        //                aSecondPos = aCardInfo[i].IndexOf("<br>", aFirstPos);

        //                String[] aStats = System.Text.RegularExpressions.Regex.Split(aCardInfo[i].Substring(aFirstPos + 2, aSecondPos - aFirstPos - 2), "/");
        //                aStrength = Convert.ToInt32(aStats[0]);
        //                aLife = Convert.ToInt32(aStats[1]);
        //                aSpeed = Convert.ToInt32(aStats[2]);

        //            }


        //            aFirstPos = aCardInfo[i].IndexOf(System.Environment.NewLine + "<br>" + System.Environment.NewLine, aSecondPos);
        //            String aCardText = aCardInfo[i].Substring(aFirstPos + 8);
        //            aCardText = aCardText.Replace("<br>", System.Environment.NewLine);



        //            mCards.AddCardRow("First Edition",-1, aName, aRarity, aCost, aThreshold, aThresholdType, aType,aStrength, aLife, aSpeed, aCardText);



        //        }

        //    }

        //}

        private void PreviousButton_Click(object sender, EventArgs e)
        {
            if (mCardSource.Position != 0)
                mCardSource.MovePrevious();

        }

        private void NextButton_Click(object sender, EventArgs e)
        {
            if (mCardSource.Position != mCardSource.Count - 1)
                mCardSource.MoveNext();
        }

        private void SearchTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                string aEditionNumber = SearchTextBox.Text.Substring(0, 1);
                string aEdition = Common.LocalDataSet.Editions.FindByNumber(Convert.ToInt32(aEditionNumber)).Name;

                

                string aEnd = SearchTextBox.Text.Substring(SearchTextBox.Text.Length - 1);
                string aCardNumber = "";
                bool aFoil = false;
                if (aEnd == "+")
                {
                    aFoil = true;
                    aCardNumber = SearchTextBox.Text.Substring(1, SearchTextBox.Text.Length - 2);
                }
                else
                {
                    aFoil = false;
                    aCardNumber = SearchTextBox.Text.Substring(1);
                }


                DataRow aRow = Common.LocalDataSet.Card.Rows.Find(new object[] { aEdition, Convert.ToInt32(aCardNumber), aFoil });
                for (int i = 0; i < mCardSource.Count; i++)
                {
                    if (((DataRowView)mCardSource[i]).Row == aRow)
                    {
                        mCardSource.Position = i;
                        break;
                    }
                }
                

                SearchTextBox.Text = "";
            }
        }

        

        int mMessageTimeout = 30000;
        Timer mTimer;
        Dictionary<ListViewItem, StatusMessage> mStatusMessages = new Dictionary<ListViewItem, StatusMessage>();
        private void AddStatusMessage(string statusMessage)
        {
            StatusMessage aMessage = new StatusMessage(statusMessage, mMessageTimeout);
            ListViewItem aItem = new ListViewItem(aMessage.Message);
            aItem.ForeColor = Color.FromArgb(255, 0, 0);
            mStatusMessages.Add(aItem, aMessage);
            StatusListView.Items.Insert(0, aItem);
            

            if (mTimer == null)
            {
                mTimer = new Timer();
                mTimer.Interval = 10;
                mTimer.Tick += new EventHandler(mTimer_Tick);
                mTimer.Start();   
            }
        }

        void mTimer_Tick(object sender, EventArgs e)
        {
            List<ListViewItem> aRemoveItems = null;
            foreach (ListViewItem aItem in StatusListView.Items)
            {
                if (mStatusMessages.ContainsKey(aItem))
                {
                    mStatusMessages[aItem].TimeLeft -= ((Timer)sender).Interval;
                    if (mStatusMessages[aItem].TimeLeft <= 0)
                    {
                        if (aRemoveItems == null)
                            aRemoveItems = new List<ListViewItem>();

                        aRemoveItems.Add(aItem);
                        mStatusMessages.Remove(aItem);
                    }
                    else
                    {
                        int aNewRedValue = (int)Math.Round(500 * ((double)(((double)mStatusMessages[aItem].TimeLeft) / ((double)mMessageTimeout))), 0);
                        if (aNewRedValue > 255)
                            aNewRedValue = 255;

                        aItem.ForeColor = Color.FromArgb(aNewRedValue, 0, 0);
                    }
                }
            }

            if (aRemoveItems != null)
            {
                foreach (ListViewItem aItem in aRemoveItems)
                {
                    StatusListView.Items.Remove(aItem);
                }
            }
        }

        private void label4_Click(object sender, EventArgs e)
        {

        }

        private void TabControl_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabControl1.SelectedTab == StatsTabPage || tabControl1.SelectedTab == InventoryTabPage)
            {
                Common.RefreshInventoryDisplayData();

                TotalCardsValueLabel.Text = Common.LocalDataSet.InventoryDisplayData.Compute("Sum(NormalCount) + Sum(FoilCount)", "").ToString();

                NormalCommonValueLabel.Text = Common.LocalDataSet.InventoryDisplayData.Compute("Sum(NormalCount)", "Rarity='Common'").ToString();
                NormalUncommonValueLabel.Text = Common.LocalDataSet.InventoryDisplayData.Compute("Sum(NormalCount)", "Rarity='Uncommon'").ToString();
                NormalRareValueLabel.Text = Common.LocalDataSet.InventoryDisplayData.Compute("Sum(NormalCount)", "Rarity='Rare'").ToString();
                NormalUltraRareValueLabel.Text = Common.LocalDataSet.InventoryDisplayData.Compute("Sum(NormalCount)", "Rarity='UltraRare'").ToString();
                NormalTotalValueLabel.Text = Common.LocalDataSet.InventoryDisplayData.Compute("Sum(NormalCount)", "").ToString();



                FoilCommonValueLabel.Text = Common.LocalDataSet.InventoryDisplayData.Compute("Sum(FoilCount)", "Rarity='Common'").ToString();
                FoilUncommonValueLabel.Text = Common.LocalDataSet.InventoryDisplayData.Compute("Sum(FoilCount)", "Rarity='Uncommon'").ToString();
                FoilRareValueLabel.Text = Common.LocalDataSet.InventoryDisplayData.Compute("Sum(FoilCount)", "Rarity='Rare'").ToString();
                FoilUltraRareValueLabel.Text = Common.LocalDataSet.InventoryDisplayData.Compute("Sum(FoilCount)", "Rarity='UltraRare'").ToString();
                FoilTotalValueLabel.Text = Common.LocalDataSet.InventoryDisplayData.Compute("Sum(FoilCount)", "").ToString();

                CharacterValueLabel.Text = Common.LocalDataSet.InventoryDisplayData.Compute("Sum(NormalCount) + Sum(FoilCount)", "Type='Character'").ToString();
                TacticValueLabel.Text = Common.LocalDataSet.InventoryDisplayData.Compute("Sum(NormalCount) + Sum(FoilCount)", "Type='Tactic'").ToString();
                ItemValueLabel.Text = Common.LocalDataSet.InventoryDisplayData.Compute("Sum(NormalCount) + Sum(FoilCount)", "Type='Item'").ToString();
                LocationValueLabel.Text = Common.LocalDataSet.InventoryDisplayData.Compute("Sum(NormalCount) + Sum(FoilCount)", "Type='Location'").ToString();

            }

            if (tabControl1.SelectedTab == PlaysetTabPage)
            {
                RefreshPlaysetDisplay();
            }


        }

        private void RefreshPlaysetDisplay()
        {
            Common.RefreshPlaysetData(CombineBetaCheckBox.Checked);

            int aOwnedPart1CommonNormal = Convert.ToInt32(Common.LocalDataSet.InventoryPlaysetData.Compute("Sum(NormalCount)", "Rarity='Common' and Edition=" + Common.FIRST_EDITION.ToString()));
            int aOwnedPart1CommonFoil = Convert.ToInt32(Common.LocalDataSet.InventoryPlaysetData.Compute("Sum(FoilCount)", "Rarity='Common' and Edition=" + Common.FIRST_EDITION.ToString()));
            int aOwnedPart1CommonTotal = Convert.ToInt32(Common.LocalDataSet.InventoryPlaysetData.Compute("Sum(TotalCount)", "Rarity='Common' and Edition=" + Common.FIRST_EDITION.ToString()));

            int aOwnedPart1Common = 0;

            if (PlaysetIncludeNormalCheckBox.Checked && PlaysetIncludeFoilCheckBox.Checked)
            {
                aOwnedPart1Common = aOwnedPart1CommonTotal;
            }
            else
            {
                if (PlaysetIncludeNormalCheckBox.Checked)
                    aOwnedPart1Common += aOwnedPart1CommonNormal;

                if (PlaysetIncludeFoilCheckBox.Checked)
                    aOwnedPart1Common += aOwnedPart1CommonFoil;
            }

            int aTotalPart1Common = (int)((Common.LocalDataSet.InventoryPlaysetData.Select("Rarity='Common' and Edition=" + Common.FIRST_EDITION.ToString()).Length) * 4);


            int aOwnedPart1UncommonNormal = Convert.ToInt32(Common.LocalDataSet.InventoryPlaysetData.Compute("Sum(NormalCount)", "Rarity='Uncommon' and Edition=" + Common.FIRST_EDITION.ToString()));
            int aOwnedPart1UncommonFoil = Convert.ToInt32(Common.LocalDataSet.InventoryPlaysetData.Compute("Sum(FoilCount)", "Rarity='Uncommon' and Edition=" + Common.FIRST_EDITION.ToString()));
            int aOwnedPart1UncommonTotal = Convert.ToInt32(Common.LocalDataSet.InventoryPlaysetData.Compute("Sum(TotalCount)", "Rarity='Uncommon' and Edition=" + Common.FIRST_EDITION.ToString()));

            int aOwnedPart1Uncommon = 0;

            if (PlaysetIncludeNormalCheckBox.Checked && PlaysetIncludeFoilCheckBox.Checked)
            {
                aOwnedPart1Uncommon = aOwnedPart1UncommonTotal;
            }
            else
            {
                if (PlaysetIncludeNormalCheckBox.Checked)
                    aOwnedPart1Uncommon += aOwnedPart1UncommonNormal;

                if (PlaysetIncludeFoilCheckBox.Checked)
                    aOwnedPart1Uncommon += aOwnedPart1UncommonFoil;
            }

            int aTotalPart1Uncommon = (int)((Common.LocalDataSet.InventoryPlaysetData.Select("Rarity='Uncommon' and Edition=" + Common.FIRST_EDITION.ToString()).Length) * 4);


            int aOwnedPart1RareNormal = Convert.ToInt32(Common.LocalDataSet.InventoryPlaysetData.Compute("Sum(NormalCount)", "Rarity='Rare' and Edition=" + Common.FIRST_EDITION.ToString()));
            int aOwnedPart1RareFoil = Convert.ToInt32(Common.LocalDataSet.InventoryPlaysetData.Compute("Sum(FoilCount)", "Rarity='Rare' and Edition=" + Common.FIRST_EDITION.ToString()));
            int aOwnedPart1RareTotal = Convert.ToInt32(Common.LocalDataSet.InventoryPlaysetData.Compute("Sum(TotalCount)", "Rarity='Rare' and Edition=" + Common.FIRST_EDITION.ToString()));

            int aOwnedPart1Rare = 0;

            if (PlaysetIncludeNormalCheckBox.Checked && PlaysetIncludeFoilCheckBox.Checked)
            {
                aOwnedPart1Rare = aOwnedPart1RareTotal;
            }
            else
            {
                if (PlaysetIncludeNormalCheckBox.Checked)
                    aOwnedPart1Rare += aOwnedPart1RareNormal;

                if (PlaysetIncludeFoilCheckBox.Checked)
                    aOwnedPart1Rare += aOwnedPart1RareFoil;
            }

            int aTotalPart1Rare = (int)((Common.LocalDataSet.InventoryPlaysetData.Select("Rarity='Rare' and Edition=" + Common.FIRST_EDITION.ToString()).Length) * 4);








            //int aOwnedPart1UltraRare = Convert.ToInt32(Common.LocalDataSet.InventoryPlaysetData.Compute("Sum(NormalCount)", "Rarity='UltraRare' and Edition=" + Common.FIRST_EDITION.ToString()));
            //int aTotalPart1UltraRare = (int)((Common.LocalDataSet.InventoryPlaysetData.Select("Rarity='UltraRare' and Edition=" + Common.FIRST_EDITION.ToString()).Length) * 4);

            int aOwnedPart1UltraRare = 0;
            //int aOwnedPart1UltraRareFoil = 0;
            int aTotalPart1UltraRare = 0;

            int aOwnedPart2CommonNormal = Convert.ToInt32(Common.LocalDataSet.InventoryPlaysetData.Compute("Sum(NormalCount)", "Rarity='Common' and Edition=" + Common.FIRST_EDITION_PART_2.ToString()));
            int aOwnedPart2CommonFoil = Convert.ToInt32(Common.LocalDataSet.InventoryPlaysetData.Compute("Sum(FoilCount)", "Rarity='Common' and Edition=" + Common.FIRST_EDITION_PART_2.ToString()));
            int aOwnedPart2CommonTotal = Convert.ToInt32(Common.LocalDataSet.InventoryPlaysetData.Compute("Sum(TotalCount)", "Rarity='Common' and Edition=" + Common.FIRST_EDITION_PART_2.ToString()));

            int aOwnedPart2Common = 0;

            if (PlaysetIncludeNormalCheckBox.Checked && PlaysetIncludeFoilCheckBox.Checked)
            {
                aOwnedPart2Common = aOwnedPart2CommonTotal;
            }
            else
            {
                if (PlaysetIncludeNormalCheckBox.Checked)
                    aOwnedPart2Common += aOwnedPart2CommonNormal;

                if (PlaysetIncludeFoilCheckBox.Checked)
                    aOwnedPart2Common += aOwnedPart2CommonFoil;
            }

            int aTotalPart2Common = (int)((Common.LocalDataSet.InventoryPlaysetData.Select("Rarity='Common' and Edition=" + Common.FIRST_EDITION_PART_2.ToString()).Length) * 4);

            
            int aOwnedPart2UncommonNormal = Convert.ToInt32(Common.LocalDataSet.InventoryPlaysetData.Compute("Sum(NormalCount)", "Rarity='Uncommon' and Edition=" + Common.FIRST_EDITION_PART_2.ToString()));
            int aOwnedPart2UncommonFoil = Convert.ToInt32(Common.LocalDataSet.InventoryPlaysetData.Compute("Sum(FoilCount)", "Rarity='Uncommon' and Edition=" + Common.FIRST_EDITION_PART_2.ToString()));
            int aOwnedPart2UncommonTotal = Convert.ToInt32(Common.LocalDataSet.InventoryPlaysetData.Compute("Sum(TotalCount)", "Rarity='Uncommon' and Edition=" + Common.FIRST_EDITION_PART_2.ToString()));

            int aOwnedPart2Uncommon = 0;

            if (PlaysetIncludeNormalCheckBox.Checked && PlaysetIncludeFoilCheckBox.Checked)
            {
                aOwnedPart2Uncommon = aOwnedPart2UncommonTotal;
            }
            else
            {
                if (PlaysetIncludeNormalCheckBox.Checked)
                    aOwnedPart2Uncommon += aOwnedPart2UncommonNormal;

                if (PlaysetIncludeFoilCheckBox.Checked)
                    aOwnedPart2Uncommon += aOwnedPart2UncommonFoil;
            }

            int aTotalPart2Uncommon = (int)((Common.LocalDataSet.InventoryPlaysetData.Select("Rarity='Uncommon' and Edition=" + Common.FIRST_EDITION_PART_2.ToString()).Length) * 4);

            int aOwnedPart2RareNormal = Convert.ToInt32(Common.LocalDataSet.InventoryPlaysetData.Compute("Sum(NormalCount)", "Rarity='Rare' and Edition=" + Common.FIRST_EDITION_PART_2.ToString()));
            int aOwnedPart2RareFoil = Convert.ToInt32(Common.LocalDataSet.InventoryPlaysetData.Compute("Sum(FoilCount)", "Rarity='Rare' and Edition=" + Common.FIRST_EDITION_PART_2.ToString()));
            int aOwnedPart2RareTotal = Convert.ToInt32(Common.LocalDataSet.InventoryPlaysetData.Compute("Sum(TotalCount)", "Rarity='Rare' and Edition=" + Common.FIRST_EDITION_PART_2.ToString()));

            int aOwnedPart2Rare = 0;

            if (PlaysetIncludeNormalCheckBox.Checked && PlaysetIncludeFoilCheckBox.Checked)
            {
                aOwnedPart2Rare = aOwnedPart2RareTotal;
            }
            else
            {
                if (PlaysetIncludeNormalCheckBox.Checked)
                    aOwnedPart2Rare += aOwnedPart2RareNormal;

                if (PlaysetIncludeFoilCheckBox.Checked)
                    aOwnedPart2Rare += aOwnedPart2RareFoil;
            }

            int aTotalPart2Rare = (int)((Common.LocalDataSet.InventoryPlaysetData.Select("Rarity='Rare' and Edition=" + Common.FIRST_EDITION_PART_2.ToString()).Length) * 4);

            //int aOwnedPart2UltraRare = Convert.ToInt32(Common.LocalDataSet.InventoryPlaysetData.Compute("Sum(NormalCount)", "Rarity='UltraRare' and Edition=" + Common.FIRST_EDITION_PART_2.ToString()));
            //int aTotalPart2UltraRare = (int)((Common.LocalDataSet.InventoryPlaysetData.Select("Rarity='UltraRare' and Edition=" + Common.FIRST_EDITION_PART_2.ToString()).Length) * 4);

            int aOwnedPart2UltraRare = 0;
            //int aOwnedPart2UltraRareFoil = 0;
            int aTotalPart2UltraRare = 0;
            


            int aOwnedPart1 = aOwnedPart1Common + aOwnedPart1Uncommon + aOwnedPart1Rare + aOwnedPart1UltraRare;;
            int aTotalPart1 = aTotalPart1Common + aTotalPart1Uncommon + aTotalPart1Rare + aTotalPart1UltraRare;

            PlaysetPart1Owned.Text = aOwnedPart1.ToString();
            PlaysetPart1Total.Text = aTotalPart1.ToString();
            PlaysetPart1Complete.Text = (Math.Round(((float)aOwnedPart1 / (float)aTotalPart1), 3) * 100).ToString() + "%";

            int aOwnedPart2 = aOwnedPart2Common + aOwnedPart2Uncommon + aOwnedPart2Rare + aOwnedPart2UltraRare;
            int aTotalPart2 = aTotalPart2Common + aTotalPart2Uncommon + aTotalPart2Rare + aTotalPart2UltraRare;

            PlaysetPart2Owned.Text = aOwnedPart2.ToString();
            PlaysetPart2Total.Text = aTotalPart2.ToString();
            PlaysetPart2Complete.Text = (Math.Round(((float)aOwnedPart2 / (float)aTotalPart2), 3) * 100).ToString() + "%";


            PlaysetPart1CommonStatus.Text = aOwnedPart1Common.ToString() + " of " + aTotalPart1Common.ToString() + " (" + (Math.Round(((float)aOwnedPart1Common / (float)aTotalPart1Common), 3) * 100).ToString() + "%)";
            PlaysetPart1UncommonStatus.Text = aOwnedPart1Uncommon.ToString() + " of " + aTotalPart1Uncommon.ToString() + " (" + (Math.Round(((float)aOwnedPart1Uncommon / (float)aTotalPart1Uncommon), 3) * 100).ToString() + "%)";
            PlaysetPart1RareStatus.Text = aOwnedPart1Rare.ToString() + " of " + aTotalPart1Rare.ToString() + " (" + (Math.Round(((float)aOwnedPart1Rare / (float)aTotalPart1Rare), 3) * 100).ToString() + "%)";

            PlaysetPart2CommonStatus.Text = aOwnedPart2Common.ToString() + " of " + aTotalPart2Common.ToString() + " (" + (Math.Round(((float)aOwnedPart2Common / (float)aTotalPart2Common), 3) * 100).ToString() + "%)";
            PlaysetPart2UncommonStatus.Text = aOwnedPart2Uncommon.ToString() + " of " + aTotalPart2Uncommon.ToString() + " (" + (Math.Round(((float)aOwnedPart2Uncommon / (float)aTotalPart2Uncommon), 3) * 100).ToString() + "%)";
            PlaysetPart2RareStatus.Text = aOwnedPart2Rare.ToString() + " of " + aTotalPart2Rare.ToString() + " (" + (Math.Round(((float)aOwnedPart2Rare / (float)aTotalPart2Rare), 3) * 100).ToString() + "%)";
        }

        private string GetTabString(int tabCount)
        {
            string aRetVal = "";
            //for (int i = 0; i < tabCount; i++)
            //{
            //    aRetVal += "\t";
            //}

            return aRetVal;
        }

        private void ExportHaveWantList()
        {

            ExportOptions aExportOptionsForm = new ExportOptions();

            if (aExportOptionsForm.ShowDialog() != DialogResult.OK)
            {
                MessageBox.Show("Export Cancelled.");
                return;
            }



            bool aHaveListIncludeCommon = aExportOptionsForm.HaveListIncludeCommon;
            bool aHaveListIncludeUncommon = aExportOptionsForm.HaveListIncludeUncommon;
            bool aHaveListIncludeRare = aExportOptionsForm.HaveListIncludeRare;
            bool aHaveListIncludeFoil = aExportOptionsForm.HaveListIncludeFoil;
            bool aHaveListSortTrade = aExportOptionsForm.HaveListSortTrade;
            bool aHaveListSortRarity = aExportOptionsForm.HaveListSortRarity;
            bool aHaveListSortEdition = aExportOptionsForm.HaveListSortEdition;


            bool aWantListIncludeCommon = aExportOptionsForm.WantListIncludeCommon;
            bool aWantListIncludeUncommon = aExportOptionsForm.WantListIncludeUncommon;
            bool aWantListIncludeRare = aExportOptionsForm.WantListIncludeRare;
            bool aWantListSortTrade = aExportOptionsForm.WantListSortTrade;
            bool aWantListSortRarity = aExportOptionsForm.WantListSortRarity;
            bool aWantListSortEdition = aExportOptionsForm.WantListSortEdition;

            bool aCombineBeta = aExportOptionsForm.CombineBetaAndFirstEdition;

            aExportOptionsForm.Dispose();
            aExportOptionsForm = null;

            string[] aTrades = new string[5] { "Arcanist", "Banker", "Gearsmith", "Rogue", "Warlord" };

            Dictionary<string, string> aTradeColors = new Dictionary<string, string>();

            aTradeColors.Add("Arcanist", "Purple");
            aTradeColors.Add("Banker", "Orange");
            aTradeColors.Add("Gearsmith", "Grey");
            aTradeColors.Add("Rogue", "DarkRed");
            aTradeColors.Add("Warlord", "Navy");


            Common.RefreshPlaysetData(false);

            StringBuilder aOutput = new StringBuilder();


            LocalData.HaveWantDataDataTable aHaveWantData = new LocalData.HaveWantDataDataTable();


            foreach (LocalData.InventoryDisplayDataRow aRow in Common.LocalDataSet.InventoryDisplayData)
            {
                if (aRow.Edition == Common.STAPLE_RESOURCE)
                    continue;

                LocalData.HaveWantDataRow aHaveWantRow = aHaveWantData.FindByCardNameEdition(aRow.CardName, aRow.Edition);


                switch (aRow.Edition)
                {
                    case Common.OPEN_BETA:
                        if (aHaveWantRow == null)
                        {
                            int aEdition = aRow.Edition;
                            string aRarity = aRow.Rarity;

                            if (aCombineBeta && aEdition == Common.OPEN_BETA)
                            {
                                DataRow[] aCardRows = Common.LocalDataSet.Card.Select("Edition=" + aEdition.ToString() + " and Name='" + aRow.CardName.Replace("'", "''") + "'");
                                if (aCardRows.Length != 0)
                                {
                                    aRarity = aCardRows[0]["Rarity"].ToString();
                                    aEdition = Common.FIRST_EDITION;
                                }
                            }

                            string aEditionName = Common.LocalDataSet.Editions.FindByNumber(aEdition).Name;

                            aHaveWantRow = aHaveWantData.AddHaveWantDataRow(aEdition, aEditionName, aRow.CardName, aRarity, aRow.NormalCount, aRow.FoilCount, 0, 0, aRow.NormalCount + aRow.FoilCount, 0, aRow.Trade, aRow.NormalCount);
                        }
                        else
                        {
                            aHaveWantRow.NormalCount += aRow.NormalCount;
                            aHaveWantRow.FoilCount += aRow.FoilCount;
                            aHaveWantRow.BetaCount += aRow.NormalCount;

                        }
                        break;
                    default:
                        if (aHaveWantRow == null)
                        {
                            aHaveWantRow = aHaveWantData.AddHaveWantDataRow(aRow.Edition, aRow.EditionName,aRow.CardName, aRow.Rarity, aRow.NormalCount, aRow.FoilCount, 0, 0, aRow.NormalCount + aRow.FoilCount, 0, aRow.Trade, 0);
                        }
                        else
                        {
                            aHaveWantRow.NormalCount += aRow.NormalCount;
                            aHaveWantRow.FoilCount += aRow.FoilCount;
                        }
                        break;

                }

                aHaveWantRow.TotalCount = aHaveWantRow.NormalCount + aHaveWantRow.FoilCount;

                if (aHaveWantRow.NormalCount > 4)
                {
                    aHaveWantRow.NormalExtra = aHaveWantRow.NormalCount - 4;
                    aHaveWantRow.NormalCount = 4;
                }

                if (aHaveWantRow.FoilCount > 4)
                {
                    aHaveWantRow.FoilExtra = aHaveWantRow.FoilCount - 4;
                    aHaveWantRow.FoilCount = 4;
                }


            }

            DataView aExportView = new DataView(aHaveWantData);
            if (aHaveListSortTrade)
                aExportView.Sort = "Trade ASC";

            if (aHaveListSortRarity && aExportView.Sort != "")
                aExportView.Sort += ",Rarity ASC";
            else
                aExportView.Sort = "Rarity ASC";

            if (aHaveListSortEdition && aExportView.Sort != "")
                aExportView.Sort += ",Edition ASC";
            else
                aExportView.Sort = "Edition ASC";

            if (aExportView.Sort != "")
                aExportView.Sort += ",CardName ASC";
            else
                aExportView.Sort = "CardName ASC";





            aOutput.Append(@"[SIZE=""4""]Have List[/SIZE]");
            aOutput.Append(System.Environment.NewLine);
            aOutput.Append(System.Environment.NewLine);

            int aTabCount = 1;
            int aCurrentEdition = -1;
            for (int i = 0; i < aTrades.Length; i++)
            {



                for (int x = aTabCount; x > 1; x--)
                {
                    aOutput.Append("[/INDENT]");
                    aTabCount -= 1;
                }
                aTabCount = 1;

                if ((aExportView.Table.Select("Trade='" + aTrades[i] + "' and NormalExtra > 0").Length > 0))
                {
                    aOutput.Append("[COLOR=\"" + aTradeColors[aTrades[i]] + "\"]");
                    aOutput.Append(System.Environment.NewLine);

                    if (aHaveListSortTrade)
                    {
                        aOutput.Append(GetTabString(aTabCount) + aTrades[i]);
                        aOutput.Append(System.Environment.NewLine);
                        aOutput.Append("[INDENT]");
                        aTabCount += 1;
                    }

                    aExportView.RowFilter = "Trade='" + aTrades[i] + "'";

                    if (aHaveListSortRarity)
                    {
                        if (aHaveListIncludeCommon && (aExportView.Table.Select("Rarity='Common' and Trade='" + aTrades[i] + "' and Trade='" + aTrades[i] + "' and NormalExtra > 0").Length > 0))
                        {

                            aOutput.Append(GetTabString(aTabCount) + "Common");
                            aOutput.Append(System.Environment.NewLine);
                            aOutput.Append("[INDENT]");
                            aTabCount += 1;




                            for (int x = 0; x < aExportView.Count; x++)
                            {
                                if (aExportView[x]["Rarity"].ToString() == "Common")
                                {
                                    if (((int)aExportView[x]["NormalExtra"]) > 0)
                                    {
                                        if (aHaveListSortEdition && (int)aExportView[x]["Edition"] != aCurrentEdition)
                                        {
                                            if (aCurrentEdition != -1)
                                            {
                                                aOutput.Append("[/INDENT]");
                                                aTabCount -= 1;
                                            }
                                            aOutput.Append(System.Environment.NewLine);
                                            aOutput.Append(GetTabString(aTabCount) + Common.LocalDataSet.Editions.FindByNumber((int)aExportView[x]["Edition"]).FullName);
                                            aOutput.Append(System.Environment.NewLine);
                                            aOutput.Append("[INDENT]");
                                            aTabCount += 1;
                                            aCurrentEdition = (int)aExportView[x]["Edition"];
                                        }

                                        aOutput.Append(GetTabString(aTabCount) + "(" + aExportView[x]["NormalExtra"].ToString() + ") " + aExportView[x]["CardName"].ToString());
                                        int aBetaCount = (int)aExportView[x]["BetaCount"];
                                        if (aBetaCount > 0)
                                            aOutput.Append(" (" + Math.Min(aBetaCount, (int)aExportView[x]["NormalExtra"]).ToString() + " Beta Available)");

                                        aOutput.Append(System.Environment.NewLine);

                                        
                                    }
                                }
                            }

                            if (aHaveListSortEdition && aCurrentEdition != -1)
                            {
                                aOutput.Append("[/INDENT]");
                                aTabCount -= 1;
                            }

                            aOutput.Append("[/INDENT]");
                            aTabCount -= 1;
                        }

                        aCurrentEdition = -1;

                        aOutput.Append(System.Environment.NewLine);

                        if (aHaveListIncludeUncommon && (aExportView.Table.Select("Rarity='Uncommon' and Trade='" + aTrades[i] + "' and NormalExtra > 0").Length > 0))
                        {

                            aOutput.Append(GetTabString(aTabCount) + "Uncommon");
                            aOutput.Append(System.Environment.NewLine);
                            aOutput.Append("[INDENT]");
                            aTabCount += 1;

                            for (int x = 0; x < aExportView.Count; x++)
                            {
                                if (aExportView[x]["Rarity"].ToString() == "Uncommon")
                                {
                                    if (((int)aExportView[x]["NormalExtra"]) > 0)
                                    {
                                        if (aHaveListSortEdition && (int)aExportView[x]["Edition"] != aCurrentEdition)
                                        {
                                            if (aCurrentEdition != -1)
                                            {
                                                aOutput.Append("[/INDENT]");
                                                aTabCount -= 1;
                                            }
                                            aOutput.Append(System.Environment.NewLine);
                                            aOutput.Append(GetTabString(aTabCount) + Common.LocalDataSet.Editions.FindByNumber((int)aExportView[x]["Edition"]).FullName);
                                            aOutput.Append(System.Environment.NewLine);
                                            aOutput.Append("[INDENT]");
                                            aTabCount += 1;
                                            aCurrentEdition = (int)aExportView[x]["Edition"];
                                        }

                                        aOutput.Append(GetTabString(aTabCount) + "(" + aExportView[x]["NormalExtra"].ToString() + ") " + aExportView[x]["CardName"].ToString());
                                        int aBetaCount = (int)aExportView[x]["BetaCount"];
                                        if (aBetaCount > 0)
                                            aOutput.Append(" (" + Math.Min(aBetaCount, (int)aExportView[x]["NormalExtra"]).ToString() + " Beta Available)");
                                        aOutput.Append(System.Environment.NewLine);
                                    }


                                }
                            }

                            if (aHaveListSortEdition && aCurrentEdition != -1)
                            {
                                aOutput.Append("[/INDENT]");
                                aTabCount -= 1;
                            }

                            aOutput.Append("[/INDENT]");
                            aTabCount -= 1;
                        }

                        aCurrentEdition = -1;
                        aOutput.Append(System.Environment.NewLine);

                        if (aHaveListIncludeRare && (aExportView.Table.Select("Rarity='Rare' and Trade='" + aTrades[i] + "' and NormalExtra > 0").Length > 0))
                        {

                            aOutput.Append(GetTabString(aTabCount) + "Rare");
                            aOutput.Append(System.Environment.NewLine);
                            aOutput.Append("[INDENT]");
                            aTabCount += 1;

                            for (int x = 0; x < aExportView.Count; x++)
                            {
                                if (aExportView[x]["Rarity"].ToString() == "Rare")
                                {
                                    if (((int)aExportView[x]["NormalExtra"]) > 0)
                                    {
                                        if (aHaveListSortEdition && (int)aExportView[x]["Edition"] != aCurrentEdition)
                                        {
                                            if (aCurrentEdition != -1)
                                            {
                                                aOutput.Append("[/INDENT]");
                                                aTabCount -= 1;
                                            }
                                            aOutput.Append(System.Environment.NewLine);
                                            aOutput.Append(GetTabString(aTabCount) + Common.LocalDataSet.Editions.FindByNumber((int)aExportView[x]["Edition"]).FullName);
                                            aOutput.Append(System.Environment.NewLine);
                                            aOutput.Append("[INDENT]");
                                            aTabCount += 1;
                                            aCurrentEdition = (int)aExportView[x]["Edition"];
                                        }

                                        aOutput.Append(GetTabString(aTabCount) + "(" + aExportView[x]["NormalExtra"].ToString() + ") " + aExportView[x]["CardName"].ToString());
                                        int aBetaCount = (int)aExportView[x]["BetaCount"];
                                        if (aBetaCount > 0)
                                            aOutput.Append(" (" + Math.Min(aBetaCount, (int)aExportView[x]["NormalExtra"]).ToString() + " Beta Available)");
                                        aOutput.Append(System.Environment.NewLine);
                                    }
                                }
                            }

                            if (aHaveListSortEdition && aCurrentEdition != -1)
                            {
                                aOutput.Append("[/INDENT]");
                                aTabCount -= 1;
                            }

                            aOutput.Append("[/INDENT]");
                            aTabCount -= 1;
                        }

                        aOutput.Append("[/COLOR]");
                        aOutput.Append(System.Environment.NewLine);
                        aCurrentEdition = -1;
                    }

                }
                else
                {


                    for (int x = 0; x < aExportView.Count; x++)
                    {
                        if (((int)aExportView[x]["NormalExtra"]) > 0)
                        {

                            if (aHaveListSortEdition && (int)aExportView[x]["Edition"] != aCurrentEdition)
                            {
                                if (aCurrentEdition != -1)
                                {
                                    aOutput.Append("[/INDENT]");
                    aTabCount -= 1;
                                }
                                aOutput.Append(System.Environment.NewLine);
                                aOutput.Append(GetTabString(aTabCount) + Common.LocalDataSet.Editions.FindByNumber((int)aExportView[x]["Edition"]).FullName);
                                aOutput.Append(System.Environment.NewLine);
                                aOutput.Append("[INDENT]");
                                aTabCount += 1;
                                aCurrentEdition = (int)aExportView[x]["Edition"];
                            }

                            aOutput.Append(GetTabString(aTabCount) + "(" + aExportView[x]["NormalExtra"].ToString() + ") " + aExportView[x]["CardName"].ToString());
                            int aBetaCount = (int)aExportView[x]["BetaCount"];
                            if (aBetaCount > 0)
                                aOutput.Append(" (" + Math.Min(aBetaCount, (int)aExportView[x]["NormalExtra"]).ToString() + " Beta Available)");
                            aOutput.Append(System.Environment.NewLine);
                        }
                    }

                    if (aHaveListSortEdition && aCurrentEdition != -1)
                    {
                        aOutput.Append("[/INDENT]");
                        aTabCount -= 1;
                    }
                }

                aCurrentEdition = -1;
                
                for (int x = aTabCount; x > 2; x--)
                {
                    aOutput.Append("[/INDENT]");
                    aTabCount -= 1;
                }
                aTabCount = 1;

                aOutput.Append(System.Environment.NewLine);
                if (aHaveListIncludeFoil && (aExportView.Table.Select("Trade='" + aTrades[i] + "' and FoilCount > 0").Length > 0))
                {

                    //Hack: Do this better later
                    if (aHaveListSortTrade)
                        aExportView.Sort = "Trade ASC";

                    //if (aHaveListSortRarity && aExportView.Sort != "")
                    //    aExportView.Sort += ",Rarity ASC";
                    //else
                    //    aExportView.Sort = "Rarity ASC";

                    if (aHaveListSortEdition && aExportView.Sort != "")
                        aExportView.Sort += ",Edition ASC";
                    else
                        aExportView.Sort = "Edition ASC";

                    if (aExportView.Sort != "")
                        aExportView.Sort += ",CardName ASC";
                    else
                        aExportView.Sort = "CardName ASC";

                    aOutput.Append("[COLOR=\"" + aTradeColors[aTrades[i]] + "\"]");
                    aOutput.Append(System.Environment.NewLine);

                    aOutput.Append(GetTabString(aTabCount) + "Foil");
                    aOutput.Append(System.Environment.NewLine);
                    aOutput.Append("[INDENT]");
                    aTabCount += 1;

                    for (int x = 0; x < aExportView.Count; x++)
                    {

                        if ((((int)aExportView[x]["FoilCount"]) + ((int)aExportView[x]["FoilExtra"])) > 0)
                        {

                            if (aHaveListSortEdition && (int)aExportView[x]["Edition"] != aCurrentEdition)
                            {
                                if (aCurrentEdition != -1)
                                {
                                    aOutput.Append("[/INDENT]");
                                    aTabCount -= 1;
                                }
                                aOutput.Append(System.Environment.NewLine);
                                aOutput.Append(GetTabString(aTabCount) + Common.LocalDataSet.Editions.FindByNumber((int)aExportView[x]["Edition"]).FullName);
                                aOutput.Append(System.Environment.NewLine);
                                aOutput.Append("[INDENT]");
                                aTabCount += 1;
                                aCurrentEdition = (int)aExportView[x]["Edition"];
                            }

                            aOutput.Append(GetTabString(aTabCount) + "(" + (((int)aExportView[x]["FoilCount"]) + ((int)aExportView[x]["FoilExtra"])).ToString() + ") " + aExportView[x]["CardName"].ToString());
                            aOutput.Append(System.Environment.NewLine);
                        }
                    }



                    aOutput.Append("[/COLOR]");
                    aOutput.Append(System.Environment.NewLine);


                }

                for (int x = aTabCount; x > 0; x--)
                {
                    aOutput.Append("[/INDENT]");
                    aTabCount -= 1;
                }

                aOutput.Append(System.Environment.NewLine);
                aOutput.Append(System.Environment.NewLine);
                aOutput.Append(System.Environment.NewLine);
                aOutput.Append(System.Environment.NewLine);


            }

            if (aWantListSortTrade)
                aExportView.Sort = "Trade ASC";

            if (aWantListSortRarity && aExportView.Sort != "")
                aExportView.Sort += ",Rarity ASC";
            else
                aExportView.Sort = "Rarity ASC";

            if (aWantListSortEdition && aExportView.Sort != "")
                aExportView.Sort += ",Edition ASC";
            else
                aExportView.Sort = "Edition ASC";

            if (aExportView.Sort != "")
                aExportView.Sort += ",CardName ASC";
            else
                aExportView.Sort = "CardName ASC";

            for (int x = aTabCount; x > 0; x--)
            {
                aOutput.Append("[/INDENT]");
                aTabCount -= 1;
            }

            aOutput.Append(@"[SIZE=""4""]Want List[/SIZE]");
            aOutput.Append(System.Environment.NewLine);
            aOutput.Append(System.Environment.NewLine);


            aCurrentEdition = -1;
            for (int i = 0; i < aTrades.Length; i++)
            {
                
                if (aExportView.Table.Select("Trade='" + aTrades[i] + "' and NormalExtra > 0").Length > 0)
                {
                    aOutput.Append("[COLOR=\"" + aTradeColors[aTrades[i]] + "\"]");
                    aOutput.Append(System.Environment.NewLine);

                    if (aWantListSortTrade)
                    {
                        aOutput.Append(GetTabString(aTabCount) + aTrades[i]);
                        aOutput.Append(System.Environment.NewLine);
                        aOutput.Append("[INDENT]");
                                aTabCount += 1;
                    }

                    aExportView.RowFilter = "Trade='" + aTrades[i] + "'";

                    if (aWantListSortRarity)
                    {
                        if (aWantListIncludeCommon && (aExportView.Table.Select("Rarity='Common' and Trade='" + aTrades[i] + "' and NormalCount < 4").Length > 0))
                        {

                            aOutput.Append(GetTabString(aTabCount) + "Common");
                            aOutput.Append(System.Environment.NewLine);
                            aOutput.Append("[INDENT]");
                                aTabCount += 1;

                            for (int x = 0; x < aExportView.Count; x++)
                            {
                                if (aExportView[x]["Rarity"].ToString() == "Common")
                                {



                                    if (((int)aExportView[x]["NormalCount"]) < 4)
                                    {
                                        if (aWantListSortEdition && (int)aExportView[x]["Edition"] != aCurrentEdition)
                                        {
                                            if (aCurrentEdition != -1)
                                            {
                                                aOutput.Append("[/INDENT]");
                                                aTabCount -= 1;
                                            }
                                            aOutput.Append(System.Environment.NewLine);
                                            aOutput.Append(GetTabString(aTabCount) + Common.LocalDataSet.Editions.FindByNumber((int)aExportView[x]["Edition"]).FullName);
                                            aOutput.Append(System.Environment.NewLine);
                                            aOutput.Append("[INDENT]");
                                            aTabCount += 1;
                                            aCurrentEdition = (int)aExportView[x]["Edition"];
                                        }

                                        aOutput.Append(GetTabString(aTabCount) + "(" + (4 - ((int)aExportView[x]["NormalCount"])).ToString() + ") " + aExportView[x]["CardName"].ToString());
                                        aOutput.Append(System.Environment.NewLine);
                                    }
                                }
                            }

                            if (aWantListSortEdition && aCurrentEdition != -1)
                            {
                                aOutput.Append("[/INDENT]");
                                aTabCount -= 1;
                            }

                            aOutput.Append("[/INDENT]");
                    aTabCount -= 1;
                        }
                        aCurrentEdition = -1;
                        aOutput.Append(System.Environment.NewLine);

                        if (aWantListIncludeUncommon && (aExportView.Table.Select("Rarity='Uncommon' and Trade='" + aTrades[i] + "' and NormalCount < 4").Length > 0))
                        {

                            aOutput.Append(GetTabString(aTabCount) + "Uncommon");
                            aOutput.Append(System.Environment.NewLine);
                            aOutput.Append("[INDENT]");
                                aTabCount += 1;

                            for (int x = 0; x < aExportView.Count; x++)
                            {
                                if (aExportView[x]["Rarity"].ToString() == "Uncommon")
                                {


                                    if (((int)aExportView[x]["NormalCount"]) < 4)
                                    {
                                        if (aWantListSortEdition && (int)aExportView[x]["Edition"] != aCurrentEdition)
                                        {
                                            if (aCurrentEdition != -1)
                                            {
                                                aOutput.Append("[/INDENT]");
                                                aTabCount -= 1;
                                            }
                                            aOutput.Append(System.Environment.NewLine);
                                            aOutput.Append(GetTabString(aTabCount) + Common.LocalDataSet.Editions.FindByNumber((int)aExportView[x]["Edition"]).FullName);
                                            aOutput.Append(System.Environment.NewLine);
                                            aOutput.Append("[INDENT]");
                                            aTabCount += 1;
                                            aCurrentEdition = (int)aExportView[x]["Edition"];
                                        }

                                        aOutput.Append(GetTabString(aTabCount) + "(" + (4 - ((int)aExportView[x]["NormalCount"])).ToString() + ") " + aExportView[x]["CardName"].ToString());
                                        aOutput.Append(System.Environment.NewLine);
                                    }
                                }
                            }

                            if (aWantListSortEdition && aCurrentEdition != -1)
                            {
                                aOutput.Append("[/INDENT]");
                                aTabCount -= 1;
                            }

                            aOutput.Append("[/INDENT]");
                    aTabCount -= 1;
                        }

                        aCurrentEdition = -1;
                        aOutput.Append(System.Environment.NewLine);

                        if (aWantListIncludeRare && (aExportView.Table.Select("Rarity='Rare' and Trade='" + aTrades[i] + "' and NormalCount < 4").Length > 0))
                        {

                            aOutput.Append(GetTabString(aTabCount) + "Rare");
                            aOutput.Append(System.Environment.NewLine);
                            aOutput.Append("[INDENT]");
                                aTabCount += 1;

                            for (int x = 0; x < aExportView.Count; x++)
                            {
                                if (aExportView[x]["Rarity"].ToString() == "Rare")
                                {
                                    

                                    if (((int)aExportView[x]["NormalCount"]) < 4)
                                    {
                                        if (aWantListSortEdition && (int)aExportView[x]["Edition"] != aCurrentEdition)
                                        {
                                            if (aCurrentEdition != -1)
                                            {
                                                aOutput.Append("[/INDENT]");
                                                aTabCount -= 1;
                                            }
                                            aOutput.Append(System.Environment.NewLine);
                                            aOutput.Append(GetTabString(aTabCount) + Common.LocalDataSet.Editions.FindByNumber((int)aExportView[x]["Edition"]).FullName);
                                            aOutput.Append(System.Environment.NewLine);
                                            aOutput.Append("[INDENT]");
                                            aTabCount += 1;
                                            aCurrentEdition = (int)aExportView[x]["Edition"];
                                        }

                                        aOutput.Append(GetTabString(aTabCount) + "(" + (4 - ((int)aExportView[x]["NormalCount"])).ToString() + ") " + aExportView[x]["CardName"].ToString());
                                        aOutput.Append(System.Environment.NewLine);
                                    }
                                }
                            }

                            if (aWantListSortEdition && aCurrentEdition != -1)
                            {
                                aOutput.Append("[/INDENT]");
                                aTabCount -= 1;
                            }

                            aOutput.Append("[/INDENT]");
                    aTabCount -= 1;
                        }

                        aOutput.Append(System.Environment.NewLine);


                    }
                    else
                    {
                        for (int x = 0; x < aExportView.Count; x++)
                        {
                            if (((int)aExportView[x]["NormalCount"]) < 4)
                            {
                                if (aWantListSortEdition && (int)aExportView[x]["Edition"] != aCurrentEdition)
                                {
                                    if (aCurrentEdition != -1)
                                    {
                                        aOutput.Append("[/INDENT]");
                    aTabCount -= 1;
                                    }
                                    aOutput.Append(System.Environment.NewLine);
                                    aOutput.Append(GetTabString(aTabCount) + Common.LocalDataSet.Editions.FindByNumber((int)aExportView[x]["Edition"]).FullName);
                                    aOutput.Append(System.Environment.NewLine);
                                    aOutput.Append("[INDENT]");
                                aTabCount += 1;
                                    aCurrentEdition = (int)aExportView[x]["Edition"];
                                }

                                aOutput.Append(GetTabString(aTabCount) + "(" + (4 - ((int)aExportView[x]["NormalCount"])).ToString() + ") " + aExportView[x]["CardName"].ToString());
                                aOutput.Append(System.Environment.NewLine);
                            }
                        }

                        if (aWantListSortEdition && aCurrentEdition != -1)
                        {
                            aOutput.Append("[/INDENT]");
                            aTabCount -= 1;
                        }
                    }

                    for (int x = aTabCount; x > 0; x--)
                    {
                        aOutput.Append("[/INDENT]");
                        aTabCount -= 1;
                    }
                    aOutput.Append("[/COLOR]");




                    aOutput.Append(System.Environment.NewLine);
                    aOutput.Append(System.Environment.NewLine);
                    aOutput.Append(System.Environment.NewLine);
                }

            }

            SaveFileDialog aFileDialog = new SaveFileDialog();
            aFileDialog.Filter = "Text File (*.txt)|*.txt";
            aFileDialog.DefaultExt = "txt";
            if (aFileDialog.ShowDialog() == DialogResult.OK)
            {
                System.IO.StreamWriter aWriter = new System.IO.StreamWriter(aFileDialog.FileName, false);
                aWriter.Write(aOutput.ToString());
                aWriter.Close();
            }
        }

        private void BackupInventoryButton_Click(object sender, EventArgs e)
        {
            SaveFileDialog aFileDialog = new SaveFileDialog();
            aFileDialog.Filter = "XML Files (*.xml)|*.xml";
            aFileDialog.DefaultExt = "xml";

            if (aFileDialog.ShowDialog() == DialogResult.OK)
            {

                string aDeleteFileName = "";

                if(System.IO.File.Exists(aFileDialog.FileName))
                {
                    
                        aDeleteFileName = System.IO.Path.GetTempFileName();
                        System.IO.File.Delete(aDeleteFileName);
                        System.IO.File.Move(aFileDialog.FileName, aDeleteFileName);

                }

                try
                {
                    System.IO.File.Copy(Common.InventoryFilePath(Application.StartupPath), aFileDialog.FileName);

                    if (aDeleteFileName != "")
                        System.IO.File.Delete(aDeleteFileName);

                }
                catch
                {
                    if (aDeleteFileName != "")
                        System.IO.File.Move(aDeleteFileName, aFileDialog.FileName);
                }

            }
            
        }

        private void ImportInventoryButton_Click(object sender, EventArgs e)
        {

        }

        private void lookupOnAthenaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.ActiveControl is DataGridView)
                {
                    DataGridView aGrid = (DataGridView)this.ActiveControl;

                    LocalData.CardRow aCardRow = Common.LocalDataSet.Card.FindByEditionNumber((int)aGrid.Rows[aGrid.CurrentCell.RowIndex].Cells["Edition"].Value, (int)aGrid.Rows[aGrid.CurrentCell.RowIndex].Cells["Number"].Value);
                    string aCardName = aCardRow.Name.Replace(" ","+");
                    System.Diagnostics.Process.Start("http://www.metatime.net/athena/search.php?exact=" + aCardName);
                }
            }
            catch { }
        }

        private void Grids_CellContextMenuStripNeeded(object sender, DataGridViewCellContextMenuStripNeededEventArgs e)
        {
            DataGridView aGrid = (DataGridView)sender;
            aGrid.Focus();
            try
            {
                aGrid.CurrentCell = aGrid.Rows[e.RowIndex].Cells[e.ColumnIndex];
            }
            catch { }
            
        }


        private void FindInventoryCardTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextBox aTextbox = (TextBox)sender;

                string aEditionNumber = aTextbox.Text.Substring(0, 1);
                string aEdition = Common.LocalDataSet.Editions.FindByNumber(Convert.ToInt32(aEditionNumber)).Name;



               string aCardNumber = aTextbox.Text.Replace("+","").Substring(1, aTextbox.Text.Length - 1);
               


                LocalData.InventoryDisplayDataRow aRow = Common.LocalDataSet.InventoryDisplayData.FindByEditionNumber(Convert.ToInt32(aEditionNumber), Convert.ToInt32(aCardNumber));
                for (int i = 0; i < mInventorySource.Count; i++)
                {
                    if (((DataRowView)mInventorySource[i]).Row == aRow)
                    {
                        mInventorySource.Position = i;
                        break;
                    }
                }

                aTextbox.SelectAll();
            }
        }

        private void Part1PlaysetGroupBox_Enter(object sender, EventArgs e)
        {

        }

        private void FindInventoryCardNameTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextBox aTextbox = (TextBox)sender;

                DataRow[] aRows = Common.LocalDataSet.Card.Select("Name Like '" + aTextbox.Text + "%'");
                if (aRows.Length == 0)
                    return;
                LocalData.InventoryDisplayDataRow aInventoryRow = Common.LocalDataSet.InventoryDisplayData.FindByEditionNumber((int)aRows[0]["Edition"], (int)aRows[0]["Number"]);

                for (int i = 0; i < mInventorySource.Count; i++)
                {
                    if (((DataRowView)mInventorySource[i]).Row == aInventoryRow)
                    {
                        mInventorySource.Position = i;
                        break;
                    }
                }

                aTextbox.SelectAll();
            }
        }

        private void CardInfoGrid_BindingContextChanged(object sender, EventArgs e)
        {
            CardInfoGrid.Columns["EditionName"].HeaderCell = new DataGridViewAutoFilterColumnHeaderCell(CardInfoGrid.Columns["EditionName"].HeaderCell);
            CardInfoGrid.Columns["Rarity"].HeaderCell = new DataGridViewAutoFilterColumnHeaderCell(CardInfoGrid.Columns["Rarity"].HeaderCell);
            CardInfoGrid.Columns["Trade"].HeaderCell = new DataGridViewAutoFilterColumnHeaderCell(CardInfoGrid.Columns["Trade"].HeaderCell);
            CardInfoGrid.Columns["Cost"].HeaderCell = new DataGridViewAutoFilterColumnHeaderCell(CardInfoGrid.Columns["Cost"].HeaderCell);
            CardInfoGrid.Columns["Threshold"].HeaderCell = new DataGridViewAutoFilterColumnHeaderCell(CardInfoGrid.Columns["Threshold"].HeaderCell);
            CardInfoGrid.Columns["ThresholdType"].HeaderCell = new DataGridViewAutoFilterColumnHeaderCell(CardInfoGrid.Columns["ThresholdType"].HeaderCell);
            CardInfoGrid.Columns["Type"].HeaderCell = new DataGridViewAutoFilterColumnHeaderCell(CardInfoGrid.Columns["Type"].HeaderCell);
            CardInfoGrid.Columns["Subtype"].HeaderCell = new DataGridViewAutoFilterColumnHeaderCell(CardInfoGrid.Columns["Subtype"].HeaderCell);
            CardInfoGrid.Columns["Strength"].HeaderCell = new DataGridViewAutoFilterColumnHeaderCell(CardInfoGrid.Columns["Strength"].HeaderCell);
            CardInfoGrid.Columns["Life"].HeaderCell = new DataGridViewAutoFilterColumnHeaderCell(CardInfoGrid.Columns["Life"].HeaderCell);
            CardInfoGrid.Columns["Speed"].HeaderCell = new DataGridViewAutoFilterColumnHeaderCell(CardInfoGrid.Columns["Speed"].HeaderCell);

        }

        private void PlaysetCheckboxes_CheckedChanged(object sender, EventArgs e)
        {
            RefreshPlaysetDisplay();
        }

        private void GenerateHaveWantButton_Click(object sender, EventArgs e)
        {
            ExportHaveWantList();
        }


        private string mLastAdded = "";
        private void AddCardTextbox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {

                int aIncrement = 1;


                if (AddCardTextbox.Text == "-")
                    AddCardTextbox.Text = "-" + mLastAdded;

                if (AddCardTextbox.Text.StartsWith("-"))
                {
                    aIncrement *= -1;
                    AddCardTextbox.Text = AddCardTextbox.Text.Substring(1);
                }
                else
                {
                    mLastAdded = AddCardTextbox.Text;
                }

                int aEditionNumber = Convert.ToInt32(AddCardTextbox.Text.Substring(0, 1));
                string aEdition = Common.LocalDataSet.Editions.FindByNumber(aEditionNumber).Name;

                string aEnd = AddCardTextbox.Text.Substring(AddCardTextbox.Text.Length - 1);
                string aCardNumber = "";
                bool aFoil = false;
                if (aEnd == "+")
                {
                    aFoil = true;
                    aCardNumber = AddCardTextbox.Text.Substring(1, AddCardTextbox.Text.Length - 2);
                }
                else
                {
                    aFoil = false;
                    aCardNumber = AddCardTextbox.Text.Substring(1);
                }

                LocalData.InventoryDisplayDataRow aRow = Common.LocalDataSet.InventoryDisplayData.FindByEditionNumber(aEditionNumber, Convert.ToInt32(aCardNumber));

                if (aFoil)
                    aRow.FoilCount += aIncrement;
                else
                    aRow.NormalCount += aIncrement;

                int aCardCount = 0;
                if (aFoil)
                    aCardCount = aRow.FoilCount;
                else
                    aCardCount = aRow.NormalCount;


                string aStatusMessage = (aIncrement > 0 ? "Added " : "Removed ") + Common.LocalDataSet.Card.FindByEditionNumber(aRow.Edition, aRow.Number).Name + (aFoil ? " Foil" : "") + " (" + aCardCount.ToString() + ")";
                AddStatusMessage(aStatusMessage);

                System.Media.SystemSounds.Asterisk.Play();

                InventoryGrid.Invalidate();

                AddCardTextbox.SelectAll();
            }
        }

        private string mLastAddedByName = "";
        private void AddCardByNameTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                TextBox aTextbox = (TextBox)sender;

                int aIncrement = 0;

                if (aTextbox.Text == "-")
                    aTextbox.Text += mLastAddedByName;

                if (aTextbox.Text.StartsWith("-"))
                {
                    aIncrement = -1;
                    aTextbox.Text = aTextbox.Text.Substring(1);
                }
                else
                {
                    aIncrement = 1;
                }

                mLastAddedByName = aTextbox.Text;

                bool aFoil = false;
                if (aTextbox.Text.EndsWith("+"))
                {
                    aFoil = true;
                    aTextbox.Text = aTextbox.Text.Substring(1, aTextbox.Text.Length - 1);
                }



                DataRow[] aRows = Common.LocalDataSet.Card.Select("Name Like '" + aTextbox.Text + "%'");
                if (aRows.Length == 0)
                {
                    AddStatusMessage("No cards found");
                    System.Media.SystemSounds.Hand.Play();
                    e.Handled = true;
                    return;
                }
                if (aRows.Length > 1)
                {
                    AddStatusMessage("Multiple cards found");
                    System.Media.SystemSounds.Hand.Play();
                    e.Handled = true;
                    return;
                }

                LocalData.InventoryDisplayDataRow aInventoryRow = Common.LocalDataSet.InventoryDisplayData.FindByEditionNumber((int)aRows[0]["Edition"], (int)aRows[0]["Number"]);

                if (aFoil)
                    aInventoryRow.FoilCount += aIncrement;
                else
                    aInventoryRow.NormalCount += aIncrement;

                int aCardCount = 0;
                if (aFoil)
                    aCardCount = aInventoryRow.FoilCount;
                else
                    aCardCount = aInventoryRow.NormalCount;

                string aStatusMessage = (aIncrement > 0 ? "Added " : "Removed ") + Common.LocalDataSet.Card.FindByEditionNumber(aInventoryRow.Edition, aInventoryRow.Number).Name + (aFoil ? " Foil" : "") + " (" + aCardCount.ToString() + ")";
                AddStatusMessage(aStatusMessage);

                System.Media.SystemSounds.Asterisk.Play();

                InventoryGrid.Invalidate();

                aTextbox.SelectAll();
            }




        }

        private void FindBoxes_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13) //Carriage Return
            {
                e.Handled = true;
            }
        }

        private void DeckBuilderButton_Click(object sender, EventArgs e)
        {
            DeckBuilder aDeckBuilderForm = new DeckBuilder();
            aDeckBuilderForm.Show();
        }

        private void reorderGridColumnsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            GridColumnOrder aColumnOrderForm = new GridColumnOrder(this.Name, (DataGridView)this.ActiveControl);
            aColumnOrderForm.ShowDialog();
        }

        private void CombineBetaCheckBox_CheckedChanged(object sender, EventArgs e)
        {
            RefreshPlaysetDisplay();
        }









       



        



       
    }
}